#! /bin/sh

##  $Id$
##
##  Make variable substitutions in configuration files.
##
##  This script does something very similar to what config.status does, but
##  it fully resolves the various path variables (prefix, exec_prefix, etc.)
##  so that they don't contain any variable substitutions.  It's easier to
##  do this in a separate script than try to use eval or the like to resolve
##  the variable names inside configure, particularly since prefix and
##  exec_prefix aren't set until the end of the script.
##
##  The original form of this script was taken from the INN project
##  which is under a BSD-style license
##

AWK="gawk"

# The paths set by configure.
prefix="/usr"
exec_prefix="${prefix}"
bindir="${exec_prefix}/bin"
libexecdir="${exec_prefix}/libexec"
libdir="/usr/lib"
sbindir="${exec_prefix}/sbin"
sysconfdir="/etc/ganglia"
includedir="${prefix}/include"

# Additional variables set by configure
CFLAGS="`echo '-g -O2 -fno-strict-aliasing -Wall -D_REENTRANT' | $AWK '{ gsub(/,/, "\\\\,"); print }'`"
LDFLAGS="`echo ' -L/usr/lib' | $AWK '{ gsub(/,/, "\\\\,"); print }'`"
LIBS="-ldl -lnsl -lnsl -lpcre -lexpat -lconfuse -lapr-1 -lpthread "
VERSION="3.1.7"
host_cpu="i686"

# Additional paths specific to Ganglia.
moduledir="/usr/lib/ganglia"
varstatedir="/var/lib"

# Additional variables that are substituted into configuration files.
GANGLIA_MAJOR_VERSION="3"
GANGLIA_MICRO_VERSION="7"
GANGLIA_MINOR_VERSION="1"
GANGLIA_VERSION="3.1.7"
REL="1"

# We can probably just assume sed is on the path, but since we have it, we may
# as well use it.
SED="/bin/sed"

input="$1"
if [ -z "$input" ] ; then
    echo "No input file specified" >&2
    exit 1
fi

output="$2"
if [ -z "$output" ] ; then
    output=`echo "$input" | $SED -e 's/\.in$//'`
fi
if [ x"$input" = x"$output" ] ; then
    echo "No output file specified and input file doesn't end in .in" >&2
    exit 1
fi

$SED -e "s,@prefix[@],$prefix,g" \
     -e "s,@exec_prefix[@],$exec_prefix,g" \
     -e "s,@bindir[@],$bindir,g" \
     -e "s,@libexecdir[@],$libexecdir,g" \
     -e "s,@libdir[@],$libdir,g" \
     -e "s,@sbindir[@],$sbindir,g" \
     -e "s,@sysconfdir[@],$sysconfdir,g" \
     -e "s,@includedir[@],$includedir,g" \
     -e "s,@CFLAGS[@],$CFLAGS,g" \
     -e "s,@LDFLAGS[@],$LDFLAGS,g" \
     -e "s,@LIBS[@],$LIBS,g" \
     -e "s,@VERSION[@],$VERSION,g" \
     -e "s,@host_cpu[@],$host_cpu,g" \
     -e "s,@moduledir[@],$moduledir,g" \
     -e "s,@varstatedir[@],$varstatedir,g" \
     -e "s,@GANGLIA_MAJOR_VERSION[@],$GANGLIA_MAJOR_VERSION,g" \
     -e "s,@GANGLIA_MICRO_VERSION[@],$GANGLIA_MICRO_VERSION,g" \
     -e "s,@GANGLIA_MINOR_VERSION[@],$GANGLIA_MINOR_VERSION,g" \
     -e "s,@GANGLIA_VERSION[@],$GANGLIA_VERSION,g" \
     -e "s,@REL[@],$REL,g" \
      < "$input" > "$output"

